package com.admin.education.controllers.business;

import com.admin.education.common.exceptions.BadRequestException;
import com.admin.education.common.response.IResponseMessage;
import com.admin.education.common.response.ResponseMessage;
import com.admin.education.common.response.ResponseMessageWithData;
import com.admin.education.common.response.Result;
import com.admin.education.dataTransferObject.PageRequest;
import com.admin.education.dataTransferObject.PageResponse;
import com.admin.education.dataTransferObject.ServicesExcuteResult;
import com.admin.education.dataTransferObject.designations.DesignationsTransfer;
import com.admin.education.dataTransferObject.designations.request.CreateDesignationsRequest;
import com.admin.education.dataTransferObject.designations.request.UpdateDesignationsRequest;
import com.admin.education.dataTransferObject.grades.GradesTransfer;
import com.admin.education.dataTransferObject.grades.Request.CreateGradeRequest;
import com.admin.education.dataTransferObject.grades.Request.UpdateGradeRequest;
import com.admin.education.services.Interfaces.IGradesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.concurrent.Callable;

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController("businessGradesController")
@RequestMapping("grades")
public class GradesController {
    @Autowired
    private IGradesService gradesService;
    @GetMapping("")
    public Callable<IResponseMessage> queryGrades(@Valid PageRequest pageRequest){
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;
                ServicesExcuteResult<PageResponse<GradesTransfer>> servicesExcuteResult = gradesService.queryGrades(pageRequest);
                if(servicesExcuteResult.isSuccess()){
                    responseMessage = new ResponseMessageWithData<PageResponse<GradesTransfer>>(Result.Success,servicesExcuteResult.getData());
                }
                else{
                    throw new BadRequestException(servicesExcuteResult.getErrorMessage());
                }
                return  responseMessage;
            }
        };
    }
    @PostMapping("")
    public Callable<IResponseMessage> createGrades(@RequestBody @Valid CreateGradeRequest createGradeRequest){
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;
                ServicesExcuteResult servicesExcuteResult = gradesService.createGrades(createGradeRequest);
                if(servicesExcuteResult.isSuccess()){
                    responseMessage = new ResponseMessage(Result.Success);
                }
                else{
                    throw  new BadRequestException(servicesExcuteResult.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
    @PutMapping("{gradesId}")
    public Callable<IResponseMessage> updateGrades(@PathVariable Long gradesId, @RequestBody @Valid UpdateGradeRequest updateGradeRequest){
        return  new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;
                ServicesExcuteResult servicesExcuteResult = gradesService.updateGrades(gradesId,updateGradeRequest);
                if(servicesExcuteResult.isSuccess()){
                    responseMessage = new ResponseMessage(Result.Success);
                }
                else{
                    throw  new BadRequestException(servicesExcuteResult.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
    @GetMapping("{id}")
    public Callable<IResponseMessage> queryGradesById(@PathVariable Long id)
    {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;
                ServicesExcuteResult<GradesTransfer> servicesExcuteResult = gradesService.queryGradesById(id);
                if(servicesExcuteResult.isSuccess()){
                    responseMessage = new ResponseMessageWithData<GradesTransfer>(Result.Success,servicesExcuteResult.getData());
                }
                else{
                    throw  new BadRequestException(servicesExcuteResult.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
    @DeleteMapping("{id}")
    public  Callable<IResponseMessage> deleteGradesById(@PathVariable Long id){
        return  ()->{
            IResponseMessage responseMessage = null;
            ServicesExcuteResult servicesExcuteResult = gradesService.deleteGrades(id);
            if(servicesExcuteResult.isSuccess()){
                responseMessage = new ResponseMessage(Result.Success);
            }
            else{
                throw  new BadRequestException(servicesExcuteResult.getErrorMessage());
            }
            return responseMessage;
        };
    }
}
